package com.luckmgr.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.Message;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.MessageService;

@Controller
@RequestMapping("/message")
public class MessageController {
	
	@Autowired
	MessageService messageService;
	
	@Autowired
	ActivityService activityServiceImpl;
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,String scode,String name,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("name", name);
		PageBean<Message> pagebean = messageService.selectListPage(map, page);
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("name", name);
		return "message/index";
	}
	
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		Message message = null;
		if(null !=id){
			message = messageService.selectByPrimaryKey(id);
			model.addAttribute("entity", message);
		}
		
		return "message/addup";
	}
	
	
	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,@ModelAttribute("entity") Message message) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(StringUtils.isNull(message.getTheme()) || StringUtils.isNull(message.getContent()) || StringUtils.isNull(message.getSeriesCode())){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "actlevel/addup";
		}
		
		
		message.setOperatorName(user.getUserName());
		message.setOperatorIp(user.getLoginIp());
		
		if(message.getId()!=null && 0!=message.getId()){
			messageService.updateEntity(message);
		}else{
			messageService.addEntity(message);
		}
		return "redirect:/message";
	}
	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			messageService.deleteByPrimaryKey(id);
		}
		return "redirect:/message";
	}
	
	
	
}
